package com.slm.report.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
import com.othershe.nicedialog.ViewConvertListener;
import com.othershe.nicedialog.ViewHolder;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;
import com.slm.mylibrary.base.BaseFragment;
import com.slm.mylibrary.utils.DialogUtils;
import com.slm.mylibrary.utils.IDialogAlertCallback;
import com.slm.report.R;
import com.slm.report.adapter.DuShuListAdapter;
import com.slm.report.databinding.FragmentAddListBinding;
import com.slm.report.db.DuShuInfoBean;
import com.slm.report.db.DuShuInfoBean_Table;
import com.slm.report.db.StudentInfoBean;
import com.slm.report.state.StudentInfoViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.ui.fragment
 * @ClassName: DegreeListFragment
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/6/3 17:37
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/3 17:37
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DegreeListFragment extends BaseFragment<StudentInfoViewModel> {
    private StudentInfoBean studentInfoBean;
    private FragmentAddListBinding viewBinding;
    private List<DuShuInfoBean> duShuInfoBeans = new ArrayList<>();
    private DuShuListAdapter duShuListAdapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = FragmentAddListBinding.inflate(LayoutInflater.from(getContext()));
        duShuListAdapter = new DuShuListAdapter(getContext(), duShuInfoBeans, new DuShuListAdapter.OnDelDataCallBack() {
            @Override
            public void deleteData(final DuShuInfoBean infoBean) {
                DialogUtils.obtainCommonDialog("确定删除信息？", new IDialogAlertCallback() {
                    @Override
                    public void sure() {
                        infoBean.delete();
                        duShuInfoBeans.remove(infoBean);
                        duShuListAdapter.notifyDataSetChanged();
                        ToastUtils.showShort("删除成功");
                        mViewModel.isChangeDu.setValue(true);
                    }
                }).show(getChildFragmentManager());
            }
        });
        viewBinding.listData.setAdapter(duShuListAdapter);
        mViewModel.studentInfoBean.observe(this, new Observer<StudentInfoBean>() {
            @Override
            public void onChanged(StudentInfoBean infoBean) {
                studentInfoBean = infoBean;
                obtainData();
            }
        });
        viewBinding.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddView();
            }
        });
        return viewBinding.getRoot();
    }


    private void obtainData() {
        SQLite.select()
                .from(DuShuInfoBean.class)
                .where(DuShuInfoBean_Table.studentId.eq(studentInfoBean.getId()))
                .orderBy(DuShuInfoBean_Table.createTime, false)
                .async()
                .queryListResultCallback(new QueryTransaction.QueryResultListCallback<DuShuInfoBean>() {
                    @Override
                    public void onListQueryResult(QueryTransaction transaction, @NonNull List<DuShuInfoBean> tResult) {
                        duShuInfoBeans.clear();
                        duShuInfoBeans.addAll(tResult);
                        duShuListAdapter.notifyDataSetChanged();
                    }
                }).execute();

    }


    private void showAddView() {
        NiceDialog.init()
                .setLayoutId(R.layout.dialog_add_du)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        Button cancelBtn = holder.getView(R.id.cancel);
                        Button sureBtn = holder.getView(R.id.sure);
                        final EditText lSphTv = holder.getView(R.id.l_sph_tv);
                        final EditText lAxTv = holder.getView(R.id.l_ax_tv);
                        final EditText lCylTv = holder.getView(R.id.l_cyl_tv);

                        final EditText rSphTv = holder.getView(R.id.r_sph_tv);
                        final EditText rAxTv = holder.getView(R.id.r_ax_tv);
                        final EditText rCylTv = holder.getView(R.id.r_cyl_tv);

                        cancelBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        sureBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (ObjectUtils.isEmpty(lSphTv.getText().toString())
                                        && ObjectUtils.isEmpty(lAxTv.getText().toString())
                                        && ObjectUtils.isEmpty(lCylTv.getText().toString())
                                        && ObjectUtils.isEmpty(rSphTv.getText().toString())
                                        && ObjectUtils.isEmpty(rAxTv.getText().toString())
                                        && ObjectUtils.isEmpty(rCylTv.getText().toString())) {
                                    ToastUtils.showShort("请输入数据");
                                    return;
                                }
                                dialog.dismiss();
                                addDuShu(lSphTv.getText().toString(), lAxTv.getText().toString(),
                                        lCylTv.getText().toString(), rSphTv.getText().toString(), rAxTv.getText().toString(), rCylTv.getText().toString());
                            }
                        });
                    }
                })
                .setOutCancel(false)
                .setAnimStyle(R.style.CenterDialogAnimation).show(getChildFragmentManager());
    }

    private void addDuShu(String lSphValue, String lAxValue, String lCylValue, String rSphValue, String rAxValue, String rCuyValue) {
        DuShuInfoBean duShuInfoBean = new DuShuInfoBean();
        duShuInfoBean.setStudentId(studentInfoBean.getId());
        duShuInfoBean.setLeftSph(lSphValue);
        duShuInfoBean.setLeftAx(lAxValue);
        duShuInfoBean.setLeftCyl(lCylValue);
        duShuInfoBean.setRightSph(rSphValue);
        duShuInfoBean.setRightAx(rAxValue);
        duShuInfoBean.setRightCyl(rCuyValue);
        duShuInfoBean.setCreateTime(System.currentTimeMillis());
        duShuInfoBean.save();
        ToastUtils.showShort("添加成功");
        mViewModel.isChangeDu.setValue(true);
        obtainData();
    }
}
